<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-20
 * Time: 下午4:17
 */
 
class BaoBeiTypeController extends Controller{

    public function index(){

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $type_name = $this->input()->post('type_name');
        $is_show = $this->input()->post('is_show');
        $filter = array();

        if($type_name){
            $filter['type_name'] = $type_name;
        }

        if($is_show || $is_show !=null){
            $filter['is_show'] = $is_show;
        }

        $list = Model::getInstance('baobeitype')->getBaoBeiTypeList($curr_page,$per_page,$filter);
        $data = array(
            'list'=>$list,
        );
        Template::show('baobei/baobeitype.php',$data);

    }

    public function baobeitypeTree(){
        
        $menu_arr = Model::getInstance('baobeitype')->getTree();
        $data = array(
            'menu_arr'=>$menu_arr,
        );
        Template::show('baobei/baobeitype_tree.php',$data);
    }

    public function add(){

        Template::show('baobei/add_baobeitype.php');
    }

    public function del(){

        $type_id = $this->input()->get('type_id');

        $type_info = Model::getInstance('baobeitype')->get($type_id);

        if($type_info['right_value'] - $type_info['left_value'] > 1){

            $data = array(
                "statusCode"=>"300",
                "message"=>"该宝贝分类下有子分类，不能删除！",
            );
            echo Response::JSON($data);
            return false;
        }

        $ret = Model::getInstance('baobeitype')->delAllCate($type_id);

        if($ret){
            $child_node = Model::getInstance('baobeitype')->getCateByLR($type_info['left_value'],$type_info['right_value']);

            $data = array(
                "statusCode"=>"200",
                "navTabId"=>"baobeitype",
            );
            echo Response::JSON($data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON($data);
        }
    }

    public function edit(){

        $type_id = (int)$this->input()->get('type_id');

        $type_info = Model::getInstance('baobeitype')->get($type_id);

        if($type_info['parent_type_id']){

            $parent_type_info = Model::getInstance('baobeitype')->get($type_info['parent_type_id']);
            
        }else{

            $parent_type_info = array(
                'type_id'=>0,
                'type_name'=>'发现宝贝'
            );
        }

        $data = array(
            'type_info'=>$type_info,
            'parent_type_info'=>$parent_type_info
        );

        Template::show('baobei/edit_baobeitype.php',$data);
    }

    public function tree(){
        $menu_arr = Model::getInstance('baobeitype')->getTree();
        $data = array(
            'menu_arr'=>$menu_arr,
        );
        Template::show('baobei/baobeitype.php',$data);
    }
}